/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.CustomTableModel;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class StationPanel extends javax.swing.JPanel {

    private Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    private ArrayList<String> colName;
    private ArrayList<ArrayList<Object>> rowData;
    private CustomTableModel ctmModel;
    private JSONObject json;
    private JSONArray jsonArray;
    private CardLayout cl;
    private String status = "ACV";
    private String mStatus = "ACV";
    private int sid;

    /**
     * Creates new form RoutePanel
     */
    public StationPanel() {
        initComponents();
        jspTableScroller.getViewport().setBackground(Color.WHITE);
        populateTableData();
        cl = (CardLayout) jpStationOperation.getLayout();
    }

    private void populateTableData() {
        new Thread() {
            @Override
            public void run() {
                colName = new ArrayList<>();
                colName.add("sid");
                colName.add("Station name");
                colName.add("Location");
                colName.add("Status");
                String data = RestServer.httpGet("station", "");
                try {
                    json = new JSONObject(data);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int status = json.getInt("status");
                        if (status == 200) {
                            rowData = new ArrayList<>();
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ArrayList<Object> temp = new ArrayList<>();
                                temp.add(jsonArray.getJSONObject(i).getInt("sid"));
                                temp.add(jsonArray.getJSONObject(i).getString("stationName"));
                                temp.add(jsonArray.getJSONObject(i).getString("location"));
                                temp.add((jsonArray.getJSONObject(i).getString("status").equals("ACV")) ? "Active" : "Inactive");
                                rowData.add(temp);
                            }
                            ctmModel = new CustomTableModel(colName, rowData);
                            jtbStation.setModel(ctmModel);
                            jtbStation.removeColumn(jtbStation.getColumnModel().getColumn(0));
                        }
                    } else {
                        Toast.makeText(getParent(), json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgAddStatus = new javax.swing.ButtonGroup();
        jbgModifyStatus = new javax.swing.ButtonGroup();
        jpStation = new javax.swing.JPanel();
        jpStationContent = new javax.swing.JPanel();
        jpOptions = new javax.swing.JPanel();
        jbtAddStation = new javax.swing.JButton();
        jbtModifyStation = new javax.swing.JButton();
        jpStationOperation = new javax.swing.JPanel();
        jpNull = new javax.swing.JPanel();
        jpModifyStation = new javax.swing.JPanel();
        jpModifyArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 20, 0, imgArrow.getWidth(this), imgArrow.getHeight(this), this);
            }
        };
        jpModify = new javax.swing.JPanel();
        jlblMStationName = new javax.swing.JLabel();
        jtfMStationName = new javax.swing.JTextField();
        jcbxMStationLocation = new javax.swing.JComboBox();
        jlblMStationLocation = new javax.swing.JLabel();
        jlblMStationStatus = new javax.swing.JLabel();
        jrbMActive = new javax.swing.JRadioButton();
        jrbMInactive = new javax.swing.JRadioButton();
        jbtMSubmit = new javax.swing.JButton();
        jbtMClear = new javax.swing.JButton();
        jpAddStation = new javax.swing.JPanel();
        jpAddArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 20, 0, imgArrow.getWidth(this), imgArrow.getHeight(this), this);
            }
        };
        jpAdd = new javax.swing.JPanel();
        jlblStationName = new javax.swing.JLabel();
        jtfStationName = new javax.swing.JTextField();
        jlblStationLocation = new javax.swing.JLabel();
        jlblStationStatus = new javax.swing.JLabel();
        jrbActive = new javax.swing.JRadioButton();
        jrbInactive = new javax.swing.JRadioButton();
        jbtClear = new javax.swing.JButton();
        jbtAdd = new javax.swing.JButton();
        jcbxStationLocation = new javax.swing.JComboBox();
        jspTableScroller = new javax.swing.JScrollPane();
        jtbStation = new javax.swing.JTable();
        jbtActivateStation = new javax.swing.JButton();
        jbtRemoveStation = new javax.swing.JButton();
        jlblStations = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jpStationArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        jpStation.setBackground(new java.awt.Color(220, 220, 220));
        jpStation.setLayout(null);

        jpStationContent.setBackground(new java.awt.Color(255, 255, 255));
        jpStationContent.setLayout(null);

        jpOptions.setBackground(new java.awt.Color(255, 255, 255));
        jpOptions.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpOptions.setLayout(null);

        jbtAddStation.setBackground(new java.awt.Color(52, 73, 94));
        jbtAddStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAddStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtAddStation.setText("Add New Station");
        jbtAddStation.setFocusPainted(false);
        jbtAddStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddStationActionPerformed(evt);
            }
        });
        jpOptions.add(jbtAddStation);
        jbtAddStation.setBounds(5, 5, 190, 45);

        jbtModifyStation.setBackground(new java.awt.Color(52, 73, 94));
        jbtModifyStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtModifyStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtModifyStation.setText("Modify Existing Station");
        jbtModifyStation.setFocusPainted(false);
        jbtModifyStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtModifyStationActionPerformed(evt);
            }
        });
        jpOptions.add(jbtModifyStation);
        jbtModifyStation.setBounds(245, 5, 190, 45);

        jpStationOperation.setBackground(new java.awt.Color(255, 255, 255));
        jpStationOperation.setLayout(new java.awt.CardLayout());

        jpNull.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpNullLayout = new javax.swing.GroupLayout(jpNull);
        jpNull.setLayout(jpNullLayout);
        jpNullLayout.setHorizontalGroup(
            jpNullLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 420, Short.MAX_VALUE)
        );
        jpNullLayout.setVerticalGroup(
            jpNullLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 365, Short.MAX_VALUE)
        );

        jpStationOperation.add(jpNull, "nullpanel");

        jpModifyStation.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyStation.setLayout(null);

        jpModifyArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpModifyArrowLayout = new javax.swing.GroupLayout(jpModifyArrow);
        jpModifyArrow.setLayout(jpModifyArrowLayout);
        jpModifyArrowLayout.setHorizontalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 190, Short.MAX_VALUE)
        );
        jpModifyArrowLayout.setVerticalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpModifyStation.add(jpModifyArrow);
        jpModifyArrow.setBounds(230, 0, 190, 10);

        jpModify.setBackground(new java.awt.Color(255, 255, 255));
        jpModify.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpModify.setLayout(null);

        jlblMStationName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMStationName.setText("Station Name");
        jpModify.add(jlblMStationName);
        jlblMStationName.setBounds(10, 20, 130, 30);

        jtfMStationName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpModify.add(jtfMStationName);
        jtfMStationName.setBounds(140, 20, 270, 30);

        jcbxMStationLocation.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jcbxMStationLocation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "THAILAND", "PERLIS", "KEDAH", "PENANG", "PERAK", "PAHANG", "KELANTAN", "TERENGGANU", "SELANGOR", "KUALA LUMPUR", "NEGERI SEMBILAN", "MELAKA", "JOHOR", "SINGAPORE" }));
        jpModify.add(jcbxMStationLocation);
        jcbxMStationLocation.setBounds(140, 70, 270, 30);

        jlblMStationLocation.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMStationLocation.setText("Station Location");
        jpModify.add(jlblMStationLocation);
        jlblMStationLocation.setBounds(10, 70, 130, 30);

        jlblMStationStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMStationStatus.setText("Operating Status");
        jpModify.add(jlblMStationStatus);
        jlblMStationStatus.setBounds(10, 120, 130, 30);

        jrbMActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgModifyStatus.add(jrbMActive);
        jrbMActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMActive.setSelected(true);
        jrbMActive.setText("Active");
        jrbMActive.setActionCommand("ACV");
        jrbMActive.setFocusPainted(false);
        jrbMActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMActiveActionPerformed(evt);
            }
        });
        jpModify.add(jrbMActive);
        jrbMActive.setBounds(160, 120, 110, 30);

        jrbMInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgModifyStatus.add(jrbMInactive);
        jrbMInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMInactive.setText("Inactive");
        jrbMInactive.setActionCommand("INV");
        jrbMInactive.setFocusPainted(false);
        jrbMInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMInactiveActionPerformed(evt);
            }
        });
        jpModify.add(jrbMInactive);
        jrbMInactive.setBounds(280, 120, 110, 30);

        jbtMSubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtMSubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtMSubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtMSubmit.setText("Submit");
        jbtMSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMSubmitActionPerformed(evt);
            }
        });
        jpModify.add(jbtMSubmit);
        jbtMSubmit.setBounds(260, 270, 150, 45);

        jbtMClear.setBackground(new java.awt.Color(39, 174, 96));
        jbtMClear.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtMClear.setForeground(new java.awt.Color(255, 255, 255));
        jbtMClear.setText("Clear Selection");
        jbtMClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMClearActionPerformed(evt);
            }
        });
        jpModify.add(jbtMClear);
        jbtMClear.setBounds(10, 270, 150, 45);

        jpModifyStation.add(jpModify);
        jpModify.setBounds(0, 10, 420, 355);

        jpStationOperation.add(jpModifyStation, "modifystation");

        jpAddStation.setBackground(new java.awt.Color(255, 255, 255));
        jpAddStation.setLayout(null);

        jpAddArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpAddArrowLayout = new javax.swing.GroupLayout(jpAddArrow);
        jpAddArrow.setLayout(jpAddArrowLayout);
        jpAddArrowLayout.setHorizontalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 190, Short.MAX_VALUE)
        );
        jpAddArrowLayout.setVerticalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpAddStation.add(jpAddArrow);
        jpAddArrow.setBounds(0, 0, 190, 10);

        jpAdd.setBackground(new java.awt.Color(255, 255, 255));
        jpAdd.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpAdd.setLayout(null);

        jlblStationName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblStationName.setText("Station Name");
        jpAdd.add(jlblStationName);
        jlblStationName.setBounds(10, 20, 130, 30);

        jtfStationName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpAdd.add(jtfStationName);
        jtfStationName.setBounds(140, 20, 270, 30);

        jlblStationLocation.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblStationLocation.setText("Station Location");
        jpAdd.add(jlblStationLocation);
        jlblStationLocation.setBounds(10, 70, 130, 30);

        jlblStationStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblStationStatus.setText("Operating Status");
        jpAdd.add(jlblStationStatus);
        jlblStationStatus.setBounds(10, 120, 130, 30);

        jrbActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAddStatus.add(jrbActive);
        jrbActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbActive.setSelected(true);
        jrbActive.setText("Active");
        jrbActive.setActionCommand("ACV");
        jrbActive.setFocusPainted(false);
        jrbActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbActiveActionPerformed(evt);
            }
        });
        jpAdd.add(jrbActive);
        jrbActive.setBounds(160, 120, 110, 30);

        jrbInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAddStatus.add(jrbInactive);
        jrbInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbInactive.setText("Inactive");
        jrbInactive.setActionCommand("INV");
        jrbInactive.setFocusPainted(false);
        jrbInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbInactiveActionPerformed(evt);
            }
        });
        jpAdd.add(jrbInactive);
        jrbInactive.setBounds(280, 120, 110, 30);

        jbtClear.setBackground(new java.awt.Color(39, 174, 96));
        jbtClear.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtClear.setForeground(new java.awt.Color(255, 255, 255));
        jbtClear.setText("Clear");
        jbtClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtClearActionPerformed(evt);
            }
        });
        jpAdd.add(jbtClear);
        jbtClear.setBounds(10, 270, 150, 45);

        jbtAdd.setBackground(new java.awt.Color(39, 174, 96));
        jbtAdd.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAdd.setForeground(new java.awt.Color(255, 255, 255));
        jbtAdd.setText("Add");
        jbtAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddActionPerformed(evt);
            }
        });
        jpAdd.add(jbtAdd);
        jbtAdd.setBounds(260, 270, 150, 45);

        jcbxStationLocation.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jcbxStationLocation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "THAILAND", "PERLIS", "KEDAH", "PENANG", "PERAK", "PAHANG", "KELANTAN", "TERENGGANU", "SELANGOR", "KUALA LUMPUR", "NEGERI SEMBILAN", "MELAKA", "JOHOR", "SINGAPORE" }));
        jpAdd.add(jcbxStationLocation);
        jcbxStationLocation.setBounds(140, 70, 270, 30);

        jpAddStation.add(jpAdd);
        jpAdd.setBounds(0, 10, 420, 355);

        jpStationOperation.add(jpAddStation, "addstation");

        jpOptions.add(jpStationOperation);
        jpStationOperation.setBounds(10, 50, 420, 365);

        jpStationContent.add(jpOptions);
        jpOptions.setBounds(5, 5, 440, 420);

        jspTableScroller.setBackground(new java.awt.Color(255, 255, 255));
        jspTableScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jtbStation.setAutoCreateRowSorter(true);
        jtbStation.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbStation.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtbStation.getTableHeader().setReorderingAllowed(false);
        jspTableScroller.setViewportView(jtbStation);

        jpStationContent.add(jspTableScroller);
        jspTableScroller.setBounds(460, 25, 395, 350);

        jbtActivateStation.setBackground(new java.awt.Color(39, 174, 96));
        jbtActivateStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtActivateStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtActivateStation.setText("Activate Station");
        jbtActivateStation.setFocusPainted(false);
        jbtActivateStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtActivateStationActionPerformed(evt);
            }
        });
        jpStationContent.add(jbtActivateStation);
        jbtActivateStation.setBounds(460, 380, 150, 45);

        jbtRemoveStation.setBackground(new java.awt.Color(39, 174, 96));
        jbtRemoveStation.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtRemoveStation.setForeground(new java.awt.Color(255, 255, 255));
        jbtRemoveStation.setText("Deactivate Station");
        jbtRemoveStation.setFocusPainted(false);
        jbtRemoveStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRemoveStationActionPerformed(evt);
            }
        });
        jpStationContent.add(jbtRemoveStation);
        jbtRemoveStation.setBounds(705, 380, 150, 45);

        jlblStations.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblStations.setText("STATIONS");
        jlblStations.setToolTipText("");
        jpStationContent.add(jlblStations);
        jlblStations.setBounds(460, 0, 395, 20);
        jpStationContent.add(jSeparator1);
        jSeparator1.setBounds(460, 20, 395, 5);

        jpStation.add(jpStationContent);
        jpStationContent.setBounds(5, 5, 860, 430);

        add(jpStation);
        jpStation.setBounds(0, 20, 870, 440);

        javax.swing.GroupLayout jpStationArrowLayout = new javax.swing.GroupLayout(jpStationArrow);
        jpStationArrow.setLayout(jpStationArrowLayout);
        jpStationArrowLayout.setHorizontalGroup(
            jpStationArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpStationArrowLayout.setVerticalGroup(
            jpStationArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpStationArrow);
        jpStationArrow.setBounds(0, 0, 150, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtActivateStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtActivateStationActionPerformed
        if (jtbStation.getSelectedRow() > -1) {
            Object id = jtbStation.getModel().getValueAt(jtbStation.getSelectedRow(), 0);
            int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to activate this station?", "Activate Station", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.YES_OPTION) {
                Map<String, Object> data = new HashMap<>();
                data.put("status", "ACV");
                String result = RestServer.httpPut("station/status", data, "" + id);
                try {
                    JSONObject delete = new JSONObject(result);
                    int statuscode = delete.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "Station successfully activated.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    }
                } catch (JSONException ex) {
                }
            }
        } else {
            Toast.makeText(this, "Please make selection first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtActivateStationActionPerformed

    private void jbtRemoveStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRemoveStationActionPerformed
        if (jtbStation.getSelectedRow() > -1) {
            Object id = jtbStation.getModel().getValueAt(jtbStation.getSelectedRow(), 0);
            int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to deactivate this station?", "Deactivate Station", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.YES_OPTION) {
                Map<String, Object> data = new HashMap<>();
                data.put("status", "INV");
                String result = RestServer.httpPut("station/status", data, "" + id);
                try {
                    JSONObject delete = new JSONObject(result);
                    int statuscode = delete.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "Station successfully deactivated.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    }
                } catch (JSONException ex) {
                }
            }
        } else {
            Toast.makeText(this, "Please make selection first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtRemoveStationActionPerformed

    private void jbtAddStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddStationActionPerformed
        jbtActivateStation.setEnabled(true);
        jbtRemoveStation.setEnabled(true);
        jbtAddStation.setBackground(new Color(44, 62, 80));
        jbtModifyStation.setBackground(new Color(52, 73, 94));
        cl.show(jpStationOperation, "addstation");
    }//GEN-LAST:event_jbtAddStationActionPerformed

    private void jrbActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbActiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbActiveActionPerformed

    private void jrbInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbInactiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbInactiveActionPerformed

    private void jbtClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtClearActionPerformed
        jtfStationName.setText("");
        jcbxStationLocation.setSelectedIndex(0);
        jrbActive.setSelected(true);
    }//GEN-LAST:event_jbtClearActionPerformed

    private void jbtAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddActionPerformed
        if (jtfStationName.getText().equals("")) {
            Toast.makeText(this, "Please fill in all fields first.", Toast.LENGTH_SHORT).display();
        } else {
            Map<String, Object> data = new HashMap<>();
            data.put("name", jtfStationName.getText().toUpperCase());
            data.put("location", jcbxStationLocation.getSelectedItem().toString());
            data.put("status", status);
            String result = RestServer.httpPost("station", data);
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statuscode = json.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "New station added.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    } else {
                        Toast.makeText(this, "Unable to add new station at the moment, please try again later.", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        }
    }//GEN-LAST:event_jbtAddActionPerformed

    private void jrbMActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMActiveActionPerformed
        mStatus = evt.getActionCommand();
    }//GEN-LAST:event_jrbMActiveActionPerformed

    private void jrbMInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMInactiveActionPerformed
        mStatus = evt.getActionCommand();
    }//GEN-LAST:event_jrbMInactiveActionPerformed

    private void jbtMSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMSubmitActionPerformed
        if (jtfMStationName.getText().equals("")) {
            Toast.makeText(this, "Please fill in all fields first.", Toast.LENGTH_SHORT).display();
        } else {
            Map<String, Object> data = new HashMap<>();
            data.put("name", jtfMStationName.getText().toUpperCase());
            data.put("location", jcbxMStationLocation.getSelectedItem().toString());
            data.put("status", mStatus);
            String result = RestServer.httpPut("station", data, ""+sid);
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statuscode = json.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "Station has been updated.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    } else {
                        Toast.makeText(this, "Station did not updated, have you modify it before submitting?", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        }
    }//GEN-LAST:event_jbtMSubmitActionPerformed

    private void jbtModifyStationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtModifyStationActionPerformed
        if (jtbStation.getSelectedRow() > -1) {
            jbtActivateStation.setEnabled(false);
            jbtRemoveStation.setEnabled(false);
            Object id = jtbStation.getModel().getValueAt(jtbStation.getSelectedRow(), 0);
            String result = RestServer.httpGet("station", id.toString());
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statuscode = json.getInt("status");
                    if (statuscode == 200) {
                        cl.show(jpStationOperation, "modifystation");
                        JSONObject data = json.getJSONObject("content");
                        jbtAddStation.setBackground(new Color(52, 73, 94));
                        jbtModifyStation.setBackground(new Color(44, 62, 80));
                        sid = data.getInt("sid");
                        jtfMStationName.setText(data.getString("stationName"));
                        jcbxMStationLocation.setSelectedItem(data.get("location"));
                        if (data.getString("status").equals("ACV")) {
                            jrbMActive.setSelected(true);
                        } else {
                            jrbMInactive.setSelected(true);
                        }
                    } else {
                        Toast.makeText(this, "Unable to get station details, please try again later.", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        } else {
            Toast.makeText(this, "Please select station from the table first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtModifyStationActionPerformed

    private void jbtMClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMClearActionPerformed
        jtbStation.clearSelection();
    }//GEN-LAST:event_jbtMClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.ButtonGroup jbgAddStatus;
    private javax.swing.ButtonGroup jbgModifyStatus;
    private javax.swing.JButton jbtActivateStation;
    private javax.swing.JButton jbtAdd;
    private javax.swing.JButton jbtAddStation;
    private javax.swing.JButton jbtClear;
    private javax.swing.JButton jbtMClear;
    private javax.swing.JButton jbtMSubmit;
    private javax.swing.JButton jbtModifyStation;
    private javax.swing.JButton jbtRemoveStation;
    private javax.swing.JComboBox jcbxMStationLocation;
    private javax.swing.JComboBox jcbxStationLocation;
    private javax.swing.JLabel jlblMStationLocation;
    private javax.swing.JLabel jlblMStationName;
    private javax.swing.JLabel jlblMStationStatus;
    private javax.swing.JLabel jlblStationLocation;
    private javax.swing.JLabel jlblStationName;
    private javax.swing.JLabel jlblStationStatus;
    private javax.swing.JLabel jlblStations;
    private javax.swing.JPanel jpAdd;
    private javax.swing.JPanel jpAddArrow;
    private javax.swing.JPanel jpAddStation;
    private javax.swing.JPanel jpModify;
    private javax.swing.JPanel jpModifyArrow;
    private javax.swing.JPanel jpModifyStation;
    private javax.swing.JPanel jpNull;
    private javax.swing.JPanel jpOptions;
    private javax.swing.JPanel jpStation;
    private javax.swing.JPanel jpStationArrow;
    private javax.swing.JPanel jpStationContent;
    private javax.swing.JPanel jpStationOperation;
    private javax.swing.JRadioButton jrbActive;
    private javax.swing.JRadioButton jrbInactive;
    private javax.swing.JRadioButton jrbMActive;
    private javax.swing.JRadioButton jrbMInactive;
    private javax.swing.JScrollPane jspTableScroller;
    private javax.swing.JTable jtbStation;
    private javax.swing.JTextField jtfMStationName;
    private javax.swing.JTextField jtfStationName;
    // End of variables declaration//GEN-END:variables
}
